package labelsystem.Controller;/**
 * @projectName careOlder
 * @Description TODO
 * @author MCG
 * @time 2023/3/7 20:05
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import labelsystem.Pojo.Doctor;
import labelsystem.Pojo.OlderUser;
import labelsystem.Pojo.Volunteer;
import labelsystem.Pojo.YoungUser;
import labelsystem.Service.*;
import labelsystem.utils.RandomUtil;
import labelsystem.utils.returnMSG;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.awt.*;
import java.util.concurrent.TimeUnit;

/**
 *description: TODO
 *@author MCG
 *@date 2023/3/7 20:05
 *@Version 1.0
 */
@Api("短信验证模块")
@RestController
@CrossOrigin
public class MsgSendHandler {
    @Resource
    private msgService msgService;
    @Resource
    private doctorService doctorService;
    @Resource
    private yuserService yuserService;
    @Resource
    private volunteerService volunteerService;
    @Resource
    private ouserService ouserService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    private boolean judgeExist(String phone,String type){
        if(type=="0"){
            return false;
        }else{
            if(type=="1"){
                return yuserService.queryInfoBytel(phone)==null?true:false;
            }else{
                if(type=="2"){
                    return doctorService.queryInfoByTel(Long.parseLong(phone))==null?true:false;
                }else{
                    if(type=="3"){
                        return volunteerService.queryInfoByTel(Long.parseLong(phone))==null?true:false;
                    }
                }
            }
        }
        return true;
    }
    @PostMapping("/registerVerifyCode")
    @ApiOperation("用户注册验证:phone   返回四位验证码")
    public returnMSG registerUser(@RequestParam("phone")String phone){
        // 从redis获取验证码，如果获取到直接返回
        String code = redisTemplate.opsForValue().get(phone);
        System.out.println(phone);
        if(!StringUtils.isEmpty(code)) {
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请勿在短时间内重复发送注册验证码");
        }
        String verifyCode= RandomUtil.getFourBitRandom();
        System.out.println(verifyCode);
        //如果没有send创造之后返回
        boolean flag=msgService.sendMsg(phone,verifyCode,0);
        redisTemplate.opsForValue().set(phone,verifyCode,5, TimeUnit.MINUTES);
        if(flag){
            returnMSG returnMSG= labelsystem.utils.returnMSG.success();
            returnMSG.add("verifyCode",verifyCode);
            return returnMSG;
        }else{
            return returnMSG.fail();
        }
    }

    @PostMapping("/resetCodeSend")
    @ApiOperation("登陆验证:phone   返回六位验证码")
    public returnMSG loginInCode(@RequestParam("phone")String phone,@RequestParam("type") String type){
//        if(judgeExist(phone,type)){
//            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
//            returnMSG.setMsg("当前用户未注册");
//            return returnMSG;
//        }
//         从redis获取验证码，如果获取到直接返回
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)) {
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请勿短时间内重复发送登陆验证码");
            return returnMSG;
        }
        String loginInCode= RandomUtil.getSixBitRandom();
        //如果没有send创造之后返回
        boolean flag=msgService.sendMsg(phone,loginInCode,1);
        redisTemplate.opsForValue().set(phone,loginInCode,5, TimeUnit.MINUTES);
        if(flag){
            returnMSG returnMSG= labelsystem.utils.returnMSG.success();
            returnMSG.add("loginInCode",loginInCode);
            return returnMSG;
        }else{
            return returnMSG.fail();
        }
    }

}
